/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_sip_analyze.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/16 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/sip/config.h"
#include "../test_sip_comm.h"
#include <sip/sip_core.h>
#include <sip/sip_header.h>
#include <sip/sip_comm.h>
#include <sip/sip_packet.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <frcomm.h>

extern void __sip_add_error_nums(L7session_sip *p_st_sess);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_sip_create_ct(&p_st_conn, TEST_SIP_SRC_IP, TEST_SIP_DST_IP, TEST_SIP_SRC_PORT, TEST_SIP_DST_PORT), FR_SUCC);

    char *p_payload = (char *) "INVITE sip:42010606001320082255_1@10.72.1.125 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "a=recvonly\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n";
    unsigned int ui_payload_len = strlen(p_payload);
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, p_payload, ui_payload_len, TEST_SIP_SRC_IP, TEST_SIP_DST_IP, TEST_SIP_SRC_PORT, TEST_SIP_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(sip_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    Frvar_field *p_st_field = NULL;
    unsigned int ui_loop = 0;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    FRHASH_LOOP(ui_loop, &st_hash_report, p_list_report)
    {
        if (p_list_report == NULL)
            continue;
        p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
        while (p_st_node_report)
        {
            p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
            if (p_st_report_data)
            {
                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "INVITE sip:42010606001320082255_1@10.72.1.125");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_METHOD, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "INVITE");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_URI, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "sip:42010606001320082255_1@10.72.1.125");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"via", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"from", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "<sip:42000000004002685574@10.72.8.120>;tag=4137830241");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"to", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "<sip:42010606001320082255_1@10.72.1.125>");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"call-id", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "3321817563@10.72.8.120");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"cseq", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "20 INVITE");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"contact", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "<sip:42000000004002685574@10.72.8.120:5000>");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"max-forwards", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "5");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"user-agent", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "mediasip/2.0");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"expires", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "120");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"subject", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "42010606001320082255:1,42000000004002685574:1");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"allow", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"content-type", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "application/sdp");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_SIP_REPORT_REQ_HDR_PREFIX"content-length", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "276");
            }
            p_st_node_report = FRLIST_NEXT_NODE(p_st_node_report);
        }
    }

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_sip_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_sip_analyze(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" sip_analyze()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
